﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Jzy.Model;
using Jzy.DAL.HR;
using System.Data;
using Jzy.Model.HR;

using Frame.Lib.Core.Base;
using Frame.Lib.Core.User;
using Frame.Lib.Core.Dept;
using Frame.Lib.Common;
using Frame.Lib.Utilities;

namespace Jzy.BLL.HR
{
    public class EmployeeBO:CommonOperate<EmployeeInfo>
    {
        private EmployeeDAL _employee;
        protected override BaseDAL DAL
        {
            get
            {
                return _employee;
            }
        }
        public EmployeeBO()
            : base("Employee")
        {
            _employee = new EmployeeDAL();
        }

        //public string GetIDByUserID(int userID)
        //{
        //    string id = _employee.GetIDByUserID(userID);
        //    return id;
        //}

        public override EmployeeInfo GetInfo(object id)
        {
            EmployeeInfo info = base.GetInfo(id);
            if (info != null)
            {
                info.GetUserInfo = userid => (new UserBO()).GetInfo(userid);
            }
            return info;
        }


       
        public System.Data.DataTable GetContactList(int deptID)
        {

            DeptBO dept = new DeptBO();
            List<int> subIDs = dept.GetSubIDs(deptID);

            DataTable dt = _employee.GetContactList(subIDs);
           
            foreach (DataRow dr in dt.Rows)
            {
                if (dr["Photo"].ToString() == "")
                {
                    dr["Photo"] = GlobalVar.WebCurrPath + "defaultface.gif";
                }
                else
                {
                    dr["Photo"] = GlobalVar.WebAppPath + dr["Photo"];
                }
            }
            return dt;
        }

        public void ExprotExcel(object dataSrc)
        {
            DataView dv;
            if (dataSrc is DataTable)
            {
                dv = ((DataTable)dataSrc).DefaultView;
            }
            else
            {
                dv = (DataView)dataSrc;
            }

            StringBuilder sb = new StringBuilder(500);
            sb.Append("<table cellspacing='0' cellpadding='0' rules='all' border='1'>");
            sb.Append("<tr>");
            sb.Append("     <th>序号</th>");
            sb.Append("     <th>姓名</th>");
            sb.Append("     <th>手机</th>");
            sb.Append("     <th>MSN</th>");
            sb.Append("     <th>电子邮件</th>");
            sb.Append("     <th>QQ</th>");
            sb.Append("</tr>");

            int index = 1;
            foreach (DataRowView row in dv)
            {
                sb.Append("<tr>");
                sb.Append("     <td>" + index + "</td>");
                sb.Append("     <td>" + row["Name"] + "</td>");
                sb.Append("     <td>" + row["Mobile"] + "</td>");
                sb.Append("     <td>" + row["OfficePhone"] + "</td>");
                sb.Append("     <td>" + row["MSN"] + "</td>");
                sb.Append("     <td>" + row["Email"] + "</td>");
                sb.Append("     <td>" + row["QQ"] + "</td>");
                sb.Append("</tr>");
            }
            sb.Append("</table>");

            WebUtil.ExportExcel(sb.ToString(), "内部通讯录");
        }
    }
}
